from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response

from django.core.paginator import Paginator

from .ser import *

from .models import *

from utils.comm import *

import random
# 加密导包
from django.contrib.auth.hashers import make_password,check_password
from utils.comm import get_redis


# Create your views here.

class LoginM(APIView):
    def get(self,request):
        # 获取手机验证码
        mobile=request.GET.get('mobile')
        yzm=random.randint(100000,999999)
        # 调用comm里的函数
        me=send_message(mobile,yzm,1)
        # 保存验证码
        coon=get_redis() # 调用封装的函数
        coon.setex(mobile,30,me) # 保存到redis
        if me:
            return Response({'code':200})

    def post(self, request):
        request.data['password'] = make_password(request.data.get('password'))
        conn = get_redis()
        print(conn.get('123123'))
        print(request.data)
        code = str(conn.get('123123'), encoding='utf8')
        if request.data.get('yzm') == conn:
            ser = LoginSer(data=request.data)
            if ser.is_valid():
                ser.save()
                return Response({'code': 200})
            else:
                return Response({'code': 10010})
        else:
            return Response({'code': 10011})
